home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 10 Scripting / 02 Berger / scc / CodeGen.H < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-16  |  2.6 KB  |  77 lines

  1. #ifndef __CodeGen_H__
  2. #define __CodeGen_H__
  3.  
  4. #include <stdio.h>
  5.  
  6. #include "Opcode.H"
  7. #include "PTNode.H"
  8.  
  9.  
  10. // This is the code generator class.  Once the parser has completed building
  11. // the parse tree, it passes the tree into this class to generate the bytecode
  12. // stream.  This class simply needs to iterate through the nodes in the parse
  13. // tree and generate all the proper instructions.
  14. class CodeGen {
  15. public:
  16.   CodeGen();
  17.   ~CodeGen();
  18.  
  19.   // This function is the only public API to this class.  It expects the root
  20.   // of the parse tree that it will generate the bytecode stream from.
  21.   void StartGen( PTNodePtr root );
  22.  
  23.  
  24. protected:
  25.   // This function is called recursively and dispatches the code generation to
  26.   // the proper handler.  This function returns the number of bytes that was
  27.   // generated for this node.
  28.   size_t Gen( PTNodePtr node );
  29.  
  30.   // These series of functions are used internally to generate the proper
  31.   // instructions for the various types of parse tree nodes.  They help break
  32.   // up the code generation logic.
  33.   void GenAdd( AddNodePtr add );
  34.   void GenSubtract( SubtractNodePtr subtract );
  35.   void GenMultiply( MultiplyNodePtr multiply );
  36.   void GenDivide( DivideNodePtr divide );
  37.   void GenConstant( ConstantNodePtr constant );
  38.   void GenIdentifier( IdentifierNodePtr identifier );
  39.   void GenAssignment( AssignmentNodePtr assignment );
  40.   void GenIf( IfNodePtr ifNode );
  41.   void GenFor( ForNodePtr forNode );
  42.   void GenStatement( StatementNodePtr stmt );
  43.   void GenBlock( BlockNodePtr block );
  44.  
  45.  
  46. protected:
  47.   // A marker is simply an abstraction away of the underlying file buffer.
  48.   // This simply notes a spot in the bytecode stream that can be later used to
  49.   // back-patch a value.  This is useful mostly for the jump instructions
  50.   // since they often do not know how far they are going to jump until later
  51.   // in the code generation.
  52.   typedef unsigned int Marker;
  53.  
  54.   // These two functions handle writing opcodes and an opcodes argument to the
  55.   // bytecode stream.
  56.   void WriteOpcode( Opcode op );
  57.   Marker WriteArg( int arg );
  58.  
  59.   // This function will update an opcode's argument at the specified marker.
  60.   // While technically, a marker can point to anywhere in the bytecode stream,
  61.   // it only logically makes sense to update opcode arguments.
  62.   void UpdateArg( Marker pos, int arg );
  63.  
  64.   // This function returns the current marker.
  65.   Marker GetCurrentMarker();
  66.  
  67.  
  68. private:
  69.   // This file handle points to the bytecode stream that the code generator is
  70.   // building.
  71.   FILE *out;
  72. };
  73.  
  74.  
  75.  
  76. #endif // __CodeGen_H__
  77.